package edu.nuc.event.controller;

import edu.nuc.event.pojo.Competition;
import edu.nuc.event.pojo.User;
import edu.nuc.event.service.ComService;
import edu.nuc.event.service.MatchService;
import edu.nuc.event.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    private ComService comService;

    @Autowired
    private MatchService matchService;

    @RequestMapping("/login")
    public String login(@RequestParam("uname") String name,
                        @RequestParam("psw") String psw,
                        Model model, HttpSession session) {
        User user = IsLogin(name, psw);

        if (user.getUsername() != null) {
            session.setAttribute("loginuser", user);
            return "index1";
        } else {
            return "fail";
        }
    }

    public User IsLogin(String account, String password) {
        String paw = DigestUtils.md5Hex(password);
        int useraccount = Integer.parseInt(account);
        User user = new User();
        List<User> users = (List<User>) userService.getAllUser();

        for (User user2 : users) {

            if (useraccount == user2.getUseraccount() && paw.equals(user2.getUserpassword())) {
                return user2;
            }
        }
        return user;

    }

    @GetMapping("/tologin")
    public String tologin() {
        return "login";
    }


    @GetMapping("/comstudent")
    public String comstudent(@RequestParam("id") int id, Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {
            List<User> users = userService.demandusers(id, 3);
            int account = userService.demanduseraccount(id, 3);
            Competition competition = comService.selectComByid(id);
            String comname = competition.getCpname();

            model.addAttribute("users", users);
            model.addAttribute("account", account);
            model.addAttribute("comname", comname);
            return "tocomstudent";
        }
    }


    @GetMapping("/comjudge")
    public String comjudge(@RequestParam("id") int id, Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {
            List<User> users = userService.demandusers(id, 2);
            int account = userService.demanduseraccount(id, 2);
            Competition competition = comService.selectComByid(id);
            String comname = competition.getCpname();

            model.addAttribute("users", users);
            model.addAttribute("account", account);
            model.addAttribute("comname", comname);

            return "tocomjudge";
        }
    }

    @GetMapping("/comadmin")
    public String comadmin(@RequestParam int id, Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {
            List<User> users = userService.demandusers(id, 1);
            int account = userService.demanduseraccount(id, 1);
            Competition competition = comService.selectComByid(id);
            String comname = competition.getCpname();

            model.addAttribute("users", users);
            model.addAttribute("account", account);
            model.addAttribute("comname", comname);

            return "tocomadmin";
        }
    }

    @GetMapping("/event-operation-admin")
    public String eventoperationadmin(@RequestParam int id, Model model, HttpSession session) {
        List<User> users = userService.demandusers(id, 1);
        int account = userService.demanduseraccount(id, 1);

        Competition competition = comService.selectComByid(id);
        String comname = competition.getCpname();

        model.addAttribute("users", users);
        model.addAttribute("account", account);
        model.addAttribute("comname", comname);
        model.addAttribute("comid", id);

        session.setAttribute("opeventid", id);

        return "event-operation-admin";
    }

    @GetMapping("/add-event-admin")
    public String addeventadmin(@RequestParam int id, Model model) {

        Competition competition = comService.selectComByid(id);
        String comname = competition.getCpname();
        model.addAttribute("comname", comname);

        List<User> users = userService.comemptyuser(1);
        model.addAttribute("users", users);

        int account = userService.comemptyuseraccount(1);
        model.addAttribute("account", account);

        return "toadd-event-admin";
    }

    @GetMapping("/doadd-event-admin")
    public String doaddeventadmin(@RequestParam int id, Model model, HttpSession session) {
        int comid = (int) session.getAttribute("opeventid");

        int i = userService.updateusercp(id, comid);

        return "redirect:/event-operation1";
    }

    @GetMapping("/del-event-admin")
    public String deleventadmin(@RequestParam int id) {
        int i = userService.updateusercpisnull(id);

        return "redirect:/event-operation1";
    }

    @GetMapping("/add-event-judge")
    public String addeventjudge(Model model, HttpSession session) {

        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {


            int comid = user.getCpid();

            Competition competition = comService.selectComByid(comid);
            String comname = competition.getCpname();
            model.addAttribute("comname", comname);

            List<User> users = userService.comemptyuser(2);
            model.addAttribute("users", users);
            int account = userService.comemptyuseraccount(2);
            model.addAttribute("account", account);

            return "toadd-event-judge";
        }
    }

    @GetMapping("/doadd-event-judge")
    public String doaddeventjudge(@RequestParam int id, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");

        int comid = user.getCpid();

        int i = userService.updateusercp(id, comid);
        return "redirect:/todetails3";
    }

    @GetMapping("/useraddcom")
    public String useraddcom(@RequestParam int id, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        int userid = user.getId();

        int i = userService.updateusercp(userid, id);


        return "redirect:/todetails4";
    }


    @GetMapping("/toupdata-user")
    public String toupaccount(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");

        model.addAttribute("user", user);
        return "/toupdata-user";
    }

    @PostMapping("/doupdate-user")
    public String doupdateaccount(User user) {


        String paw = DigestUtils.md5Hex(user.getUserpassword());
        user.setUserpassword(paw);
        int i = userService.updateuser(user);

        return "redirect:/tologin";

    }

    @GetMapping("/toselect-student1")
    public String toselectstudent1(Model model) {


        List<User> userList = userService.findusers(3);


        model.addAttribute("users", userList);

        return "toselect-user";
    }

    @GetMapping("/toselect-judge1")
    public String toselectjudge1(Model model) {
        List<User> userList = userService.findusers(2);
        model.addAttribute("users", userList);
        return "toselect-user";
    }

    @GetMapping("/toselect-admin1")
    public String toselectadmin1(Model model) {
        List<User> userList = userService.findusers(1);
        model.addAttribute("users", userList);
        return "toselect-user";
    }

    @GetMapping("/tourist")
    public String tourist() {
        return "index0";
    }

    @GetMapping("/toadd-user")
    public String toadduser() {
        return "toadd-user";
    }

    @PostMapping("/doadd-user")
    public String doadduser(User user) {
        String paw = DigestUtils.md5Hex(user.getUserpassword());
        user.setUserpassword(paw);
        int userccount = user.getUseraccount();
        int i = userService.adduser(user);

        if (i > 0) {
            int userid = userService.selectByuseraccount(userccount);


            int y = matchService.addmatchuser(userid);

            return "suss";
        } else {
            return "fail";
        }

    }


}
